<template>
    <div id="loginSe" style="height: 100%;background-color: aliceblue;">
      <!-- <iframe id="myIframewpslogin" :src="iframeSrcRs" style="width: 100%;height: 100%;" frameborder="0"
        ref="myIframewpslogin"></iframe> -->
        <button @click="postToken">send token</button>
    </div>
  </template>
  
  <script>
  
  export default {
    name: 'loginSe',
    data () {
      return {
        iframeSrcRs: 'http://151.101.7.4/sso/?service=http://151.0.191.134:9607/sso/user/loginCallback', //真正登录页 CA
        token: ''
      }
    },
    mounted () {
      const iframe = this.$refs.myIframewpslogin;
      if(iframe){
        iframe.addEventListener('load', () => {
        // 在这里可以选择直接调用send方法，或者后续其他地方调用send时先检查这个标志位
        this.send();
      });
      }
      
    },
    created () {
      // console.log('window.location.href', window.location.href)
      let arr = window.location.href.split('?')
      if (arr && arr.length >= 2) {
        if (arr[1].indexOf('token=') !== -1) {
          // console.log('77', arr[1])
          let tokenArr = arr[1].split('=')
          // console.log(tokenArr[1])
          if (tokenArr[1]) {
            // window.alert(tokenArr[1])
            window.localStorage.setItem('htoken', tokenArr[1])
            this.$nextTick(() => {
              this.send()
            })
          }
        }
      }
    },
    methods: {
      send () {
        window.parent.postMessage({
          token: window.localStorage.getItem('htoken') || ''
        }, '*')
      },
  
      // 
      postToken () {
        window.parent.postMessage({
          token: 'eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEzMTU4MDU1MDMwIiwidHlwZSI6IndlYiIsImVudiI6Im1kIiwiaWF0IjoxNzQxMjU0NDQ0fQ.sastjMKzbJVIr2FmaHOwksHO5aLipIjuqBXsCPB_QFM'
        }, '*')
      }
    }
  }
  </script>
  